package com.xidian.zhiancloud.service.ypp.messageCenter.notice;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.NoticeMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.Information;
import com.xidian.zhiancloud.model.Notice;
import com.xidian.zhiancloud.model.NoticeExample;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    public List<Notice> getById(String status) {
        NoticeExample noticeExample = new NoticeExample();
        NoticeExample.Criteria criteria = noticeExample.createCriteria();
        criteria.andStatusEqualTo(status);
        List<Notice> noticeList = noticeMapper.selectByExample(noticeExample);
        if (noticeList == null) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        return noticeList;
    }

    public void updateByIds(List<Notice> noticeList) {
        for (Notice notice:noticeList){
            Notice notice1 = new Notice();
            BeanUtils.copyProperties(notice,notice1);
            notice1.setStatus("1");
            int result = noticeMapper.updateByPrimaryKey(notice1);
            if (result == 0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    }

    public Object getInformation(Long id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        if (notice == null){
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        if(notice.getStatus().equals("0")) {
        	Notice notice1 = new Notice();
            notice1.setStatus("1");
            notice1.setId(id);
            int result = noticeMapper.updateByPrimaryKeySelective(notice1);
        }
        
        return notice;
    }

	public Page<Notice> getNoticeList(Notice info) {
		// TODO Auto-generated method stub
		PageHelper.startPage(info.getPageNum(), info.getPageSize());
		Page<Notice> pagedata =noticeMapper.getNoticeList(info);
		return pagedata;
	}

	public void deleteTNoticeByIds(Long[] ids) {
		// TODO Auto-generated method stub
		noticeMapper.deleteTNoticeByIds(ids);
	}

	public void updateTNoticeByIds(Long[] ids) {
		// TODO Auto-generated method stub
		noticeMapper.updateTNoticeByIds(ids);
	}

	public void insertInfo(Notice info) {
		// TODO Auto-generated method stub
		noticeMapper.insertInfo(info);
	}
}
